package com.haidong.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalTime;
import java.time.Period;

@Component
@Aspect
public class ProjectAdvice {
    @Pointcut("execution(* com.haidong.service.*Service.*(..))")
    private void servicePt(){}

    /**
     * 使用环绕切入点完成万次运行效率测试
     * 由于要执行1万次,查看效率就不使用返回值了
     * @param pjp
     * @return
     * @throws Throwable
     */
    @Around("servicePt()")
    public void runSpeed(ProceedingJoinPoint pjp) throws Throwable {
        // 为了在测试类中获取并显示当前方法信息
        // 获取签名信息,进而获取执行操作名称(接口名),最后获取方法名
        Signature signature = pjp.getSignature();
        String className = signature.getDeclaringTypeName();
        String methodName = signature.getName();

        LocalTime start = LocalTime.now();
        for (int i = 0; i < 10000; i++) {
            pjp.proceed();
        }
        LocalTime stop = LocalTime.now();

        System.out.println("万次执行:" + className + "." + methodName + "-->" + Duration.between(start,stop));
    }
}
